var request = require('request');
var cheerio = require('cheerio');

request({
    url: 'https://www.zhihu.com/?time='+Date.now(),
    headers: {
        'Referer': 'https://www.zhihu.com/signup?next=%2F',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6'
    }
}, function(err, response, body) {
    if(err) throw err;
    if(response.statusCode === 200) {
        var $ = cheerio.load(body);
        
        // console.log($('.nav').html());
        console.log(body);
        var data = [];
        $('[data-za-detail-view-element_name=Title]').each(function(){
            var $this = $(this);
                
            // 使用trim去掉数据两端的空格
            data.push(trim($this.html()));
        });

        console.log(data);

    }
});

// 删除字符串左右两端的空格
function trim(str){ 
    return str.replace(/(^\s*)|(\s*$)/g, "");
}